import 'dart:async';

import 'package:flutter/material.dart';
import 'package:umeng_analytics_plugin/umeng_analytics_plugin.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver{
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {


    var result = await UmengAnalyticsPlugin.init(
      androidKey: '',
      iosKey: '',
      logEnabled: true
    );

    print('Umeng initialized.');

    if (!mounted) {
      return;
    }

    // setState(() {
    //   _platformVersion = result! ? 'OK' : 'ERROR';
    // });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeDependencies();
    switch (state) {
    // 应用程序可见，前台
      case AppLifecycleState.resumed:
        print('App测试开关回归前台，判断是否重启');
        UmengAnalyticsPlugin.pageResume();
        break;
    // 处于这种状态的应用程序应该假设它们可能在任何时候暂停。
      case AppLifecycleState.inactive:
        break;
    // 应用程序不可见，后台
      case AppLifecycleState.paused:
        UmengAnalyticsPlugin.pagePause();
        break;
      case AppLifecycleState.detached:
        break;
    }
  }
}
